﻿var pagecount = "0";
var num = 0;
var selectnum = 0;
var page = 1; //页数
var refurl = document.referrer.toString();

$(document).ready(function () {
    shareload(page);
    shows();
    if ("wap" == getUrlArgStr("sources")) {
        $(".wamtop").css("display", "none");
    }
    $(window).bind("scroll", function () {
        //当滚动条滚动时
        var scollTop = $(this).scrollTop() + $(this).height();
        var scollH = $(document).height();

        if (scollH - scollTop < 100) {
            if (wait.isShow) { return; } //如果正在加载，则返回  
            page++;
            shareload(page);
        }
        return false;
    });

    $(".topright").click(function () {
        $(".listcheck").each(function () {
            if ($(this).css("background-color").toString() != "rgba(0, 0, 0, 0)") {
                if ($("#checkid").val() == "") $("#checkid").val($(this).children().attr("value"));
                $("#check_ids").val($(this).children().attr("value") + "," + $("#check_ids").val());
            }
        });

        if ($("#checkid").val() == "") {
            Alert("请选择广告");
            return;
        }
        showsq();
    });
});

//加载数据
function shareload(pages) {
    if (pagecount == "1") { return; }
    if (!islogin()) { window.location.href = 'login.html'; return; }
    ajax.Getadvertlist("page_index=" + pages, function (data) {
        if (data.item.pageCount < pages) {
            if (pagecount == "0")
                Alert('到底了！');
            pagecount = "1"; return;
        }
        $(data.item.table).each(function () {
            num++;
            var float = "";
            if (this.type == 1) { float = "顶部"; } else if (this.type == 2) { float = "底部"; } else { float = "悬浮"; }
            var titles = this.title;
            if (titles.length > 12)
                titles = titles.substr(0, 12) + "...";
            $(".ShareList").append("<div class=\"selectlist\"><div class=\"advertimg\"><img src=\"img/9.png\" /></div><div class=\"advertnumber\">" + num + "</div><div class=\"advetname\">" + titles + "</div><div class=\"listimg\"><img src=\"" + this.img + "\"  /><div class='locationtxt'>" + float + "</div></div><div class=\"listcheck\" select=\"0\"><input type=\"hidden\" name=\"checkid\" value=\"" + this.id + "\"  /><img src=\"img/6.png\" /></div></div>");


        });

        $(".listcheck").off();
        $(".listcheck").bind('click', function () {

            if ($(this).attr("select").toString() == "1") {            
                $(this).attr("select", "0");
                $(this).css("background-color", "");
                selectnum--;
                return;
            }
            if (selectnum > 2) { Alert("最多选择三张图片"); return; }
            if ($(this).attr("select").toString() == "0") {
                $(this).attr("select", "1");
                $(this).css("background-color", "#F37B0B");
                selectnum++;
                return;
            }
        });


    });
}

function shows() {
    $(".Sq").click(function () {
        window.location.href = "Sumitapp.html";
    });
    $(".Csq").click(function () {
        $(".Pb").css("display", "none");
        $("body").css("overflow", "auto");
        $(".ShowSel").css("display", "none");
    });
}

function showsq() {
    ajax.getAjax("ajax/advert.ashx?IsChange", "advert_id=" + $("#checkid").val() + "&article_id=" + getUrlArgStr("article_id"), function (data) {
        if (data.code.toString() == "0") {
            var article_id = getUrlArgStr("article_id");
            var param = "is_app=1&change=1";
            if (article_id != "") {
                param += "&type=1"; //站内文章
                param += "&advert_id=" + $("#checkid").val();
                param += "&article_id=" + getUrlArgStr("article_id");
            }
            else {
                param += "&type=2"; //URL外链文章
                param += "&advert_id=" + $("#checkid").val();
                param += "&url_title=" + getUrlArgStr("title");
                param += "&url=" + getUrlArgStr("url");
            }
            AddShare(userid); //添加分享链接，并跳转
        }
        else {
            if (data.msg != "")
                $("#errmsg").html(data.msg);
            return false;
        }
    });
}

function getUrl(values) {
    try {
        var q = refurl.substr(1);
        var qd = q.split('?')[1];
        var qs = qd.split('&');
        var argStr = '';
        if (qs) {
            for (var i = 0; i < qs.length; i++) {
                if (values == qs[i].substring(0, qs[i].indexOf('='))) {
                    argStr = qs[i].substring(qs[i].indexOf('=') + 1);
                    break;
                }
            }
        }
        return argStr;
    }
    catch (e) { return ""; }
}

//更换广告后，产生新的分享链接
function AddShare(aduser_id) {
    var shareid = 0;
    var advertId = $("#checkid").val();
    var articleId = getUrlArgStr("article_id");
    var shareid = getUrlArgStr("shareid");
    var url = getUrlArgStr("url");
    var title = getUrlArgStr("title");
    var type = getUrlArgStr("type");
    var logo = getUrlArgStr("logo");
    if ((!isNaN(articleId)) && parseInt(articleId) > 0)
        type = "1";

    //如果用户已经登录 则先保存用户分享的信息
    var param = "advert_id=" + advertId + "&shareid=" + shareid + "&type=" + type + "&aduser_id=" + aduser_id + "&adp_id=" + getCookie("parentid") + "&advert_ids=" + $("#check_ids").val();
    if (type == 2)
        param += "&url=" + url + "&url_title=" + title + "&url_logo=" + logo;
    else
        param += "&article_id=" + articleId;

    ajax.AddShare(param, function (data) {
        if (data.code == 0) {
            if (data.item != null && data.item != "")
                shareid = data.item;
            Alert("可以分享文章啦");
            setTimeout(function () { GoPage(shareid, type); }, 500);
        }
        else if (data.code == 1)
            Alert(data.msg)
        else
            toast.show(data.msg);
    });
    return shareid;
}

function GoPage(shareid, type) {
    var param = "is_app=1&type=" + type + "&advert_id=" + $("#checkid").val() + "&article_id=" + getUrlArgStr("article_id") + "&isadd=1";
    if (shareid > 0)
        param += "&shareid=" + shareid;
    window.location.href = "detail.aspx?" + param;
}
